// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RegionAccessibilityChecker.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Numerics
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    /// <summary>
    /// A class that maintains inaccessible regions and checks whether a given 
    /// point is within one of these inaccessible regions
    /// </summary>
    [DataContract]
    public class RegionAccessibilityChecker
    {
        /// <summary>
        /// a list of regions represented by boxes which are inaccessible
        /// </summary>
        [DataMember]
        private List<BoxRegion> inaccessibleBoxList;

        /// <summary>
        /// a list of regions represented by half spaces whose negative sides are inaccessible
        /// </summary>
        [DataMember]
        private List<HalfSpaceRegion> inaccessibleHalfSpaceList;

        /// <summary>
        /// Initializes a new instance of the <see cref="RegionAccessibilityChecker" /> class
        /// </summary>
        public RegionAccessibilityChecker()
        {
            this.inaccessibleBoxList = new List<BoxRegion>();
            this.inaccessibleHalfSpaceList = new List<HalfSpaceRegion>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RegionAccessibilityChecker" /> class
        /// with given lists of boxes or half spaces, when a given list is null, the constructor
        /// will initialize a list on its own
        /// </summary>
        /// <param name="inaccessibleBoxes">a list of box regions that are inaccessible</param>
        /// <param name="inaccessibleHalfSpaces">a list of half spaces that are inaccessible</param>
        public RegionAccessibilityChecker(List<BoxRegion> inaccessibleBoxes, List<HalfSpaceRegion> inaccessibleHalfSpaces)
        {
            if (null != inaccessibleBoxes)
            {
                this.inaccessibleBoxList = inaccessibleBoxes;
            }
            else
            {
                this.inaccessibleBoxList = new List<BoxRegion>();
            }

            if (null != inaccessibleHalfSpaces)
            {
                this.inaccessibleHalfSpaceList = inaccessibleHalfSpaces;
            }
            else
            {
                this.inaccessibleHalfSpaceList = new List<HalfSpaceRegion>();
            }
        }

        /// <summary>
        /// add one box region
        /// </summary>
        /// <param name="b">a region with a box shape</param>
        public void AddInaccessibleRegion(BoxRegion b)
        {
            this.inaccessibleBoxList.Add(b);
        }

        /// <summary>
        /// add one half space region
        /// </summary>
        /// <param name="hs">a half space defined by a separating plane</param>
        public void AddInaccessibleRegion(HalfSpaceRegion hs)
        {
            this.inaccessibleHalfSpaceList.Add(hs);
        }

        /// <summary>
        /// clear all the accessible regions
        /// </summary>
        public void ClearAllInaccessibleRegions()
        {
            this.inaccessibleBoxList.Clear();
            this.inaccessibleHalfSpaceList.Clear();
        }

        /// <summary>
        /// given a point, specified inside Pose p, check whether it is within one of the
        /// regions that are not supposed to be visited, if it is outside any of these regions
        /// return true (safe), otherwise false (unsafe)
        /// </summary>
        /// <param name="p">the point</param>
        /// <returns>whether the point is safe to visit or not</returns>
        public bool IsSafe(Vector3 p)
        {
            // check against all the box regions
            foreach (BoxRegion b in this.inaccessibleBoxList)
            {
                if (b.Contains(p))
                {
                    return false;
                }
            }

            // check against all the half space regions
            foreach (HalfSpaceRegion hs in this.inaccessibleHalfSpaceList)
            {
                if (hs.IsOnTheNegativeSide(p))
                {
                    return false;
                }
            }

            return true;
        }
    }
}
